package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyCatalog;
import cn.wolfcode.wolf2w.query.StrategyCatalogQuery;
import cn.wolfcode.wolf2w.repostiory.StrategyCatalogRepository;
import cn.wolfcode.wolf2w.service.IStrategyCatalogService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DBHelper;
import cn.wolfcode.wolf2w.vo.CatalogVo;
import com.sun.org.apache.bcel.internal.generic.NEW;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.aggregation.AggregationResults;
import org.springframework.data.mongodb.core.aggregation.TypedAggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
//@Transactional  暂时先别管事务
public class StrategyCatalogServiceImpl implements IStrategyCatalogService {

    @Autowired
    private StrategyCatalogRepository repository;

    @Autowired
    private MongoTemplate template;

    @Autowired
    private IStrategyService strategyService;

    @Override
    public StrategyCatalog get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public Page<StrategyCatalog> query(StrategyCatalogQuery qo) {
        Query query = new Query();
        Pageable pageable = PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize(),
                Sort.Direction.ASC, "_id");
        return DBHelper.query(template, StrategyCatalog.class, query,pageable);
    }
    @Override
    public void saveOrUpdate(StrategyCatalog region) {
        if(!StringUtils.hasLength(region.getId())){
            //添加
            region.setId(null);
            repository.save(region);
        }else{

          repository.save(region);

        }
    }


    @Override
    public void delete(String id) {
        repository.deleteById(id);
    }

    @Override
    public List<StrategyCatalog> list() {
        List<StrategyCatalog> list = repository.findAll();
        return list;
    }

    @Override
    public List<CatalogVo> groupList() {
        List<CatalogVo> list = new ArrayList<>();
        TypedAggregation<StrategyCatalog> agg = Aggregation.newAggregation(StrategyCatalog.class,
                Aggregation.group("destName").
                        push("name").as("names").
                        push("id").as("ids")
        );
        AggregationResults<Map> result = template.aggregate(agg,Map.class);
        List<Map> datas = result.getMappedResults();
        for (Map data : datas) {
            CatalogVo vo = new CatalogVo();
            vo.setDestName(data.get("_id").toString());

            List<Object> names = (List<Object>) data.get("names");
            List<Object> ids = (List<Object>) data.get("ids");

            List<StrategyCatalog> catalogList = new ArrayList<>();

            for (int i = 0;i < names.size(); i++) {
                StrategyCatalog sc  = new StrategyCatalog();
                String name = names.get(i).toString();
                String id = ids.get(i).toString();
                sc.setId(id);
                sc.setName(name);
                catalogList.add(sc);

            }

            vo.setCatalogList(catalogList);
            list.add(vo);
        }
        System.out.println(list);
        return list;
    }

    @Override
    public List<StrategyCatalog> queryCatalogByDestId(String destId) {
       //
        List<StrategyCatalog> list = new ArrayList<>();
        list= repository.findByDestId(destId);
        for (StrategyCatalog strategyCatalog : list) {
        List<Strategy> strategylist = strategyService.findByCatalogId(strategyCatalog.getId());
        strategyCatalog.setStrategies(strategylist);
        }

        return list;
    }


}
